<?php

class DefaultListAction extends FrontendAction
{

    public function __construct()
    {
        parent::__construct();
        $this->responseType = 'tpl';
        $this->layout = 'index';
    }

    public function execute()
    {
        
        $keyword = $this->context->get('k');
        if (empty($keyword))
        {
            $keyword = $this->context->post('k');
        }
        $keyword = str_replace('/', '', $keyword);
        $cat = $this->context->get('c');
        $orderby = $this->context->get('o');
        if (!in_array($orderby, array(1, 2, 3, 4, 5, 6, 7)))
        {
            $orderby = 1;
        }
        $orderbys = array(1 => 'commissionNum_desc', 2 => 'commissionVolume_desc', 3 => 'credit_desc', 4 => 'delistTime_desc', 5 => 'commissionRate_desc', 6 => 'price_asc', 7 => 'price_desc');

        $pageSize = 40;
        $con = array();
        $con['page_size'] = $pageSize;

        if ($keyword != '')
        {

            if (strpos($keyword, '得一健') !== false)
            {
                header('location:/mall/');
            }
            if (function_exists('mb_detect_encoding'))
            {
                $defaultEncode = $this->data['config']['system']['encode'];
                if (!empty($defaultEncode) && $defaultEncode != 'UTF-8')
                {
                    $encode = mb_detect_encoding($keyword, C('CommonFunction')->getDefaultArray($defaultEncode, array('EUC-CN', 'ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5')));
                    if ($encode != 'UTF-8')
                    {
                        $keyword = iconv($encode, 'UTF-8', $keyword);
                    }
                }
            }
            $con['keyword'] = $keyword;
            //����ΪSearch8�ؼ��
            $this->data['config']['search8']['keyword'] = $keyword;
        }
        //��Ŀ����
        if ($cat > 0)
        {
            $con['cid'] = $cat;
        }

        if (array_key_exists('cid', $con))
        {
            $cat = $con['cid'];
        }
        else
        {

            $cat = 0;
        }

        $categoryName = '';
        $categoryService = C('CategoryService');
        $categoryList = $categoryService->getCategoryListByPId($cat);
        if ($cat > 0)
        {
            $parentCategoryList = $categoryService->getParentList($cat);
            if ($parentCategoryList)
            {
                $this->data['parentCategoryList'] = $parentCategoryList;
                $categoryName = $parentCategoryList[count($parentCategoryList) - 1]['name'];
                //��Ŀ������
                if ($parentCategoryList[count($parentCategoryList) - 1]['is_hide'] == 1)
                {
                    $this->layout = 'result';
                    $this->code = 403;
                    return;
                }
            }
        }

        $price = $this->context->get('price');
        $prices = array(1 => array('start_price' => 1, 'end_price' => 50), 2 => array('start_price' => 50, 'end_price' => 100), 3 => array('start_price' => 100, 'end_price' => 200), 4 => array('start_price' => 200, 'end_price' => 500), 5 => array('start_price' => 500, 'end_price' => 1000), 6 => array('start_price' => 1000, 'end_price' => 2000), 7 => array('start_price' => 2000, 'end_price' => 5000), 8 => array('start_price' => 5000, 'end_price' => 500000),);
        if (is_numeric($price) && array_key_exists($price, $prices))
        {
            $con['start_price'] = $prices[$price]['start_price'];
            $con['end_price'] = $prices[$price]['end_price'];
        }
        $this->data['con'] = $con;
        $this->data['con']['orderby'] = $orderby;
        $this->data['con']['price'] = $price;

        $page = $this->getPage();
        $ltConfig = C('LtConfig');
        $pagerConfig = $ltConfig->get('pagination.pager');

        $pagerConfig['per_page'] = $pageSize;

        $pagerConfig['num_links_show'] = 8;

        $ltConfig->addConfig(array('pagination.pager' => $pagerConfig));

        $num = $pageSize;
        $start = ($page - 1) * $pageSize;
        $con['page_no'] = $page;
        $con['page_size'] = $pageSize;
        $con['sort'] = $orderbys[$orderby];
        //��Ʒ
        $productService = new ProductService();
        $productService->searchTaoKeProducts($con);
        $totalNum = $productService->getTotalProcuct();
        $productList = $productService->getProductList();
        $this->data['totalNum'] = $totalNum;
        $this->data['productList'] = $productList;

        $baseUrl = C('LtUrl')->generate('Default', 'List', array('k' => $keyword, 'c' => $cat, 'o' => $orderby, 'price' => $price, 'p' => ':page'));

        $pagination = new PiscesPagination;
        $pagination->init();
        $this->data['pages'] = $pagination->pager($page, $totalNum, $baseUrl);

        //����
        $orderLink = array();
        $orderLink['select'] = $orderby;
        $orderLink['num'] = C('LtUrl')->generate('Default', 'List', array('k' => $keyword, 'c' => $cat, 'o' => 1, 'p' => $page));
        $orderLink['money'] = C('LtUrl')->generate('Default', 'List', array('k' => $keyword, 'c' => $cat, 'o' => 2, 'p' => $page));
        $orderLink['credit'] = C('LtUrl')->generate('Default', 'List', array('k' => $keyword, 'c' => $cat, 'o' => 3, 'p' => $page));
        $orderLink['publish'] = C('LtUrl')->generate('Default', 'List', array('k' => $keyword, 'c' => $cat, 'o' => 4, 'p' => $page));
        $orderLink['rate'] = C('LtUrl')->generate('Default', 'List', array('k' => $keyword, 'c' => $cat, 'o' => 5, 'p' => $page));
        $orderLink['priceasc'] = C('LtUrl')->generate('Default', 'List', array('k' => $keyword, 'c' => $cat, 'o' => 6, 'p' => $page));
        $orderLink['pricedesc'] = C('LtUrl')->generate('Default', 'List', array('k' => $keyword, 'c' => $cat, 'o' => 7, 'p' => $page));


        $this->data['orderby'] = $orderLink;


        if ($categoryList)
        {
            foreach ($categoryList as &$clv)
            {
                $clv['link'] = C('LtUrl')->generate('Default', 'List', array('k' => $keyword, 'c' => $clv['id'], 'o' => $orderby, 'p' => 1));
            }
        }
        $this->data['categoryList'] = $categoryList;

        $hotkeyService = C('HotkeyService');
        //修改记录 2011.3.28
        if ($this->data['isRobot'] || $keyword == '')
        {            
            if ($cat > 0)
            {
                $hotkeyList = $hotkeyService->getListByCategoryId($cat);
                if(count($hotkeyList)>20)
                {
                    array_splice($hotkeyList,18);
                }
                $this->data['hotkeyList'] = $hotkeyList;
            }
        }

        //���������
        if ($keyword != '' && $totalNum > 0 && !$this->data['isRobot'])
        {
            if (empty($cat))
            {
                $cat = 0;
            }
            $tmp = $hotkeyService->getList(array('category_id' => $cat, 'name' => $keyword), '', 0, 1);
            if (empty($tmp['rows']))
            {
                $hotkeyService->add(array('category_id' => $cat, 'name' => $keyword, 'orderby' => 0));
            }
        }

        if ($page == 1)
        {
            $page = '';
        }

        //修改记录 2011.3.29
       // $seoConfig = C('SettingService')->get('seo_setting', true);
        $seoConfig=array(
            'listtitle'=>'商城正品{catName}{keyword}{page}',
            'listkeyword'=>'商城{catName}{keyword}|正品{catName}{keyword}|热卖{catName}{keyword}',
            'listdescription'=>'网上商城2011年热卖的正品{catName}{keyword}'
        );

        $seoTitle = $this->getSeoStr($seoConfig['listtitle'], array('keyword' => $keyword, 'catName' => $categoryName, 'page' => $page));
        $seoKeyword = $this->getSeoStr($seoConfig['listkeyword'], array('keyword' => $keyword, 'catName' => $categoryName, 'page' => $page));
        $seoDescription = $this->getSeoStr($seoConfig['listdescription'], array('keyword' => $keyword, 'catName' => $categoryName, 'page' => $page));

        $this->data['seo']['title'] = $seoTitle;
        $this->data['seo']['keyword'] = $seoKeyword;
        $this->data['seo']['description'] = $seoDescription;
    }

}
